﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QuanLyKhachSan
{
    public partial class frmLapDanhMucPhong : Form
    {
        private int flag;
        public frmLapDanhMucPhong()
        {
            InitializeComponent();
        }
        private bool KiemTraNhapLieu()
        {
            if (string.IsNullOrEmpty(txtTenPhong.Text) == true)
            {
                MessageBox.Show("Ten phong khong hop le", "Thong bao ");
                txtTenPhong.Focus();
                return false;
            }
            if (PhongDTO.KiemTraTenPhong(txtTenPhong.Text) == false)
            {
                MessageBox.Show("Ten phong khong hop le", "Thong bao ");
                txtTenPhong.Focus();
                return false;
            }
            return true;
        } 
        //load danh sach len datagridview
        private void LoadDanhSachPhongLenGridView(DataTable dt)
        {
            dgvDanhSachPhong.Rows.Clear();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];

                                       
                    dgvDanhSachPhong.Rows.Add();


                    dgvDanhSachPhong.Rows[i].Cells["STT"].Value = (i + 1).ToString();
                    dgvDanhSachPhong.Rows[i].Cells["maPhong"].Value = dr["maPhong"].ToString();
                    dgvDanhSachPhong.Rows[i].Cells["phong"].Value = dr["tenPhong"].ToString();
                    dgvDanhSachPhong.Rows[i].Cells["loaiPhong"].Value = dr["tenLoaiPhong"].ToString();
                    dgvDanhSachPhong.Rows[i].Cells["donGia"].Value = dr["donGia"].ToString();
                    dgvDanhSachPhong.Rows[i].Cells["ghiChu"].Value = dr["ghiChu"].ToString();
                }
                //dgvDanhSachPhong.Rows[0].Selected = true;
            }

        }
        private void LoadFrom()
        {
            DataTable ds = LoaiPhongBUS.LayDanhSachLoaiPhong();
            //DataTable ds1 = LoaiPhongBUS.LayDanhSachLoaiPhong1();
            cboLoaiPhong.DataSource = ds;
            cboLoaiPhong.ValueMember = "MaLoaiPhong";
            cboLoaiPhong.DisplayMember = "TenLoaiPhong";
            cboLoaiPhong.SelectedIndex=0;

            cboLoaiPhongTK.ValueMember = "MaLoaiPhong";
            cboLoaiPhongTK.DisplayMember = "TenLoaiPhong";
            cboLoaiPhongTK.DataSource = ds;
            
            cboLoaiPhongTK.SelectedIndex=0;
            string tenTK = cboLoaiPhong.Text;
            int maloai = (int)cboLoaiPhong.SelectedValue;
            DataTable dt = PhongBUS.LayDanhSachPhongTheoLoai(maloai);//int.Parse(LoaiPhongBUS.LayMaPhongTheoTen(tenTK));
            LoadDanhSachPhongLenGridView(dt);
            dgvDanhSachPhong.ClearSelection();
            
            //dgvDanhSachPhong.DataSource = PhongBUS.LayDanhSachPhongTheoLoai(int.Parse(LoaiPhongBUS.LayMaPhongTheoTen(tenTK)));
        }
        private void frmLapDanhMucPhong_Load(object sender, EventArgs e)
        {
            //Lấy giờ hệ thống
            string gio = DateTime.Now.ToLongTimeString();
            string ngay = DateTime.Now.ToLongDateString();
            string chuoi = ngay + " " + gio;
            txtNgayHienTai.Text = chuoi;
            LoadFrom();
            dgvDanhSachPhong.ClearSelection();
           

            txtTenPhong.Enabled = false;
            cboLoaiPhong.Enabled = false;
            txtGhiChu.Enabled = false;
            btnXoa.Enabled = true;
            btnCapNhat.Enabled = true;
            btnThem.Enabled = true;
            btnLuu.Enabled = false;
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            flag = 0;
            txtTenPhong.Text = "";
            txtGhiChu.Text = "";
            txtTenPhong.Enabled = true;
            cboLoaiPhong.Enabled = true;
            txtGhiChu.Enabled = true;
            btnLuu.Enabled = true;
            btnCapNhat.Enabled = false;
            btnThem.Enabled = false;
            btnXoa.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachPhong.SelectedRows.Count > 0)
            {
                txtTenPhong.Enabled = false;
                cboLoaiPhong.Enabled = false;
                txtGhiChu.Enabled = false;
                //btnLuu.Enabled = true;
                btnThem.Enabled = true;
                btnCapNhat.Enabled = true;
                DialogResult result = MessageBox.Show("Ban chac chan muon xoa khong?", "Thong Bao", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    DataGridViewRow row = dgvDanhSachPhong.SelectedRows[0];
                    int maPhong = int.Parse(row.Cells["maPhong"].Value.ToString());
                    PhongBUS.Delete(maPhong);
                    btnLuu.Enabled = false;
                    frmLapDanhMucPhong_Load(sender, e);
                }
            }
            else
                MessageBox.Show("Vui Long Chon Phong Muon Xoa");
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {

            flag = 1;
            txtTenPhong.Enabled = true;
            cboLoaiPhong.Enabled = true;
            txtGhiChu.Enabled = true;
            btnThem.Enabled = false;
            btnXoa.Enabled = false;
            btnCapNhat.Enabled = false;
            btnLuu.Enabled = true;
            //}

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            
            if (flag == 0)
            {
                if (KiemTraNhapLieu() == true)
                {
                    PhongDTO phong = new PhongDTO();
                    DialogResult result = MessageBox.Show("Ban chac chan muon them khong?", "Thong Bao", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        phong.MaLoaiPhong = (int)cboLoaiPhong.SelectedValue;// int.Parse(LoaiPhongBUS.LayMaPhongTheoTen(cboLoaiPhong.SelectedValue.ToString()));
                        phong.TenPhong = txtTenPhong.Text;
                        phong.TinhTrang = 0;
                        phong.GhiChu = txtGhiChu.Text;
                        phong.DanhDau = 0;
                        PhongBUS.Insert(phong);  
                    }
                    frmLapDanhMucPhong_Load(sender, e);
                    
                }
            }
            else
            {
                if (flag == 1)
                {
                    if (dgvDanhSachPhong.SelectedRows.Count > 0)
                    {
                        if (KiemTraNhapLieu() == true)
                        {
                            PhongDTO phong = new PhongDTO();
                            DialogResult result = MessageBox.Show("Ban chac chan muon cap nhat khong?", "Thong Bao", MessageBoxButtons.YesNo);
                            if (result == DialogResult.Yes)
                            {
                                DataGridViewRow row = dgvDanhSachPhong.SelectedRows[0];
                                phong.MaLoaiPhong = (int)cboLoaiPhong.SelectedValue; //int.Parse(LoaiPhongBUS.LayMaPhongTheoTen(cboLoaiPhong.SelectedValue.ToString()));
                                phong.TenPhong = txtTenPhong.Text;
                                phong.GhiChu = txtGhiChu.Text;
                                phong.MaPhong = int.Parse(row.Cells["maPhong"].Value.ToString());
                                PhongBUS.Update(phong);
                                
                            }
                            frmLapDanhMucPhong_Load(sender, e);
                        }
                    }
                    else
                        MessageBox.Show("Vui Long Chon Phong Muon Cap Nhat");
                }
            }
            
        }

        private void dgvDanhSachPhong_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvDanhSachPhong.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dgvDanhSachPhong.SelectedRows[0];
                if (row.Cells["phong"].Value != null)
                {
                    txtTenPhong.Text = row.Cells["phong"].Value.ToString();
                    txtGhiChu.Text = row.Cells["ghiChu"].Value.ToString();
                    //cboLoaiPhong.SelectedIndex = cboLoaiPhongTK.SelectedIndex;
                }
            }
            else
            {
                txtGhiChu.Text = "";
                txtTenPhong.Text = "";
            }
        }

        private void cboLoaiPhongTK_TextChanged(object sender, EventArgs e)
        {
            int index = cboLoaiPhongTK.SelectedIndex;
            if (index != -1)
            {
                int ma = (int)cboLoaiPhongTK.SelectedValue;
                //string tenTK = cboLoaiPhongTK.Text;
                DataTable dt = PhongBUS.LayDanhSachPhongTheoLoai(ma);//int.Parse(LoaiPhongBUS.LayMaPhongTheoTen(tenTK)));
                LoadDanhSachPhongLenGridView(dt);
                dgvDanhSachPhong.ClearSelection();
                //dgvDanhSachPhong.DataSource = PhongBUS.LayDanhSachPhongTheoLoai(int.Parse(LoaiPhongBUS.LayMaPhongTheoTen(tenTK)));
            }
        }

        private void cboLoaiPhong_TextChanged(object sender, EventArgs e)
        {
            dgvDanhSachPhong.ClearSelection();
            txtTenPhong.Text = "";
            txtGhiChu.Text = "";
        }
        
    }
}